package kubernetes

import (
	"context"
	"encoding/json"
	"errors"
	"fmt"
	"k8s-admin/global"
	"k8s-admin/utils"
	"time"

	appsv1 "k8s.io/api/apps/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

type StatefulSet struct {
	Items []appsv1.StatefulSet
	Total int
}

func (stat *k8sCluster) CreateClusterStatefulSetYaml(cid, Namespace string, StatefulSetData string) error {
	cache, err := utils.GetCache(cid)
	if err != nil {
		global.TPLogger.Error(err)
		return err
	} else {
		var (
			ctx    context.Context
			cancel context.CancelFunc
		)

		ctx, cancel = context.WithTimeout(context.TODO(), time.Second*5)
		defer cancel()
		dest, err := utils.DecodeBase64(StatefulSetData)
		if err != nil {
			global.TPLogger.Error(err)
			return err
		} else {
			state := &appsv1.StatefulSet{}
			err := json.Unmarshal([]byte(dest), state)
			if err != nil {
				global.TPLogger.Error(err)
				return err
			} else {
				_, err = cache.AppsV1().StatefulSets(Namespace).Create(ctx, state, metav1.CreateOptions{})
				if err != nil {
					global.TPLogger.Error("创建StatefulSet失败：", err.Error())
					return err
				} else {
					return nil
				}
			}
		}
	}
}

func (stat *k8sCluster) GetClusterStatefulSetList(cid, Namespace string) (*StatefulSet, error) {
	cache, err := utils.GetCache(cid)
	var (
		ctx    context.Context
		cancel context.CancelFunc
	)
	ctx, cancel = context.WithTimeout(context.TODO(), time.Second*10)
	defer cancel()
	list, err := cache.AppsV1().StatefulSets(Namespace).List(ctx, metav1.ListOptions{})
	if err != nil {
		return nil, err
	} else {
		return &StatefulSet{
			Items: list.Items,
			Total: len(list.Items),
		}, nil
	}
}

func (stat *k8sCluster) GetClusterStatefulSet(cid, Namespace, StatefulSetName string) (*appsv1.StatefulSet, error) {
	cache, err := utils.GetCache(cid)
	if err != nil {
		global.TPLogger.Error("Node管理模块获取节点:", errors.New("当前集群不存在"))
		return nil, err
	} else {
		var (
			ctx    context.Context
			cancel context.CancelFunc
		)
		ctx, cancel = context.WithTimeout(context.TODO(), time.Second*5)
		defer cancel()
		get, err := cache.AppsV1().StatefulSets(Namespace).Get(ctx, StatefulSetName, metav1.GetOptions{})
		if err != nil {

			global.TPLogger.Error("获取StatefulSet:", err)
			fmt.Printf("err: %v\n", err)
			return nil, err
		} else {

			return get, err
		}
	}
}

func (stat *k8sCluster) DeleteClusterStatefulSet(cid, Namespace, StatefulSetName string) error {
	var (
		ctx    context.Context
		cancel context.CancelFunc
	)

	cache, err := utils.GetCache(cid)
	ctx, cancel = context.WithTimeout(context.TODO(), time.Second*10)

	defer cancel()
	err = cache.AppsV1().StatefulSets(Namespace).Delete(ctx, StatefulSetName, metav1.DeleteOptions{})
	if err != nil {
		return err
	} else {
		return nil
	}
}

func (stat *k8sCluster) UpdateClusterStatefulSet(cid, Namespace, StatefulSetData string) error {
	var (
		ctx    context.Context
		cancel context.CancelFunc
	)
	cache, err := utils.GetCache(cid)

	ctx, cancel = context.WithTimeout(ctx, time.Second*5)
	defer cancel()
	dest, err := utils.DecodeBase64(StatefulSetData)
	if err != nil {
		return err
	} else {
		stat := &appsv1.StatefulSet{}

		err := json.Unmarshal([]byte(dest), stat)
		if err != nil {
			return err
		}
		_, err = cache.AppsV1().StatefulSets(Namespace).Update(ctx, stat, metav1.UpdateOptions{})
		if err != nil {
			global.TPLogger.Error(err.Error())
			return errors.New("StatefulSet资源更新失败")
		} else {
			return nil
		}
	}
}
